<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>c++的知识(1) | heit0ng</title><meta name="author" content="heit0ng"><meta name="copyright" content="heit0ng"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="最近加了个项目组，需要用到这方面的内容，安全方面可能就直接鸽了 C++语法一类的就不赘述了，和C基本差不多 C++面向对象c++类&amp;对象类是C++的核心特性，通常被称为用户定义的类型 类用于指定对象的形式，它包含了数据表示法核用于处理数据的方法，类中的数据和方法被称为类的成员。函数在一个类中被称为类的成员 c++类定义 例子： 1234567class Box&amp;#123;	pubilc:">
<meta property="og:type" content="article">
<meta property="og:title" content="c++的知识(1)">
<meta property="og:url" content="https://heit0ng.gitee.io/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/index.html">
<meta property="og:site_name" content="heit0ng">
<meta property="og:description" content="最近加了个项目组，需要用到这方面的内容，安全方面可能就直接鸽了 C++语法一类的就不赘述了，和C基本差不多 C++面向对象c++类&amp;对象类是C++的核心特性，通常被称为用户定义的类型 类用于指定对象的形式，它包含了数据表示法核用于处理数据的方法，类中的数据和方法被称为类的成员。函数在一个类中被称为类的成员 c++类定义 例子： 1234567class Box&amp;#123;	pubilc:">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg">
<meta property="article:published_time" content="2022-03-12T13:48:26.310Z">
<meta property="article:modified_time" content="2023-03-22T12:34:14.929Z">
<meta property="article:author" content="heit0ng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://heit0ng.gitee.io/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'c++的知识(1)',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-22 20:34:14'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><hr/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">heit0ng</a></span><div id="menus"><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">c++的知识(1)</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-03-12T13:48:26.310Z" title="发表于 2022-03-12 21:48:26">2022-03-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-22T12:34:14.929Z" title="更新于 2023-03-22 20:34:14">2023-03-22</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="c++的知识(1)"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>最近加了个项目组，需要用到这方面的内容，安全方面可能就直接鸽了</p>
<p>C++语法一类的就不赘述了，和C基本差不多</p>
<h2 id="C-面向对象"><a href="#C-面向对象" class="headerlink" title="C++面向对象"></a>C++面向对象</h2><h3 id="c-类-amp-对象"><a href="#c-类-amp-对象" class="headerlink" title="c++类&amp;对象"></a>c++类&amp;对象</h3><p>类是C++的核心特性，通常被称为用户定义的类型</p>
<p>类用于指定对象的形式，它包含了数据表示法核用于处理数据的方法，类中的数据和方法被称为类的成员。函数在一个类中被称为类的成员</p>
<h4 id="c-类定义"><a href="#c-类定义" class="headerlink" title="c++类定义"></a><strong>c++类定义</strong></h4><p><img src="https://s2.loli.net/2022/03/12/K6NoIwGuHi9y3pd.png" alt="1号.png"></p>
<p>例子：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class Box</span><br><span class="line">&#123;</span><br><span class="line">	pubilc:</span><br><span class="line">		double length;</span><br><span class="line">		double breadth;</span><br><span class="line">		double height;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>public在类的外部是可访问的，关于private和protected后续会介绍</p>
<h4 id="定义C-对象"><a href="#定义C-对象" class="headerlink" title="定义C++对象"></a>定义C++对象</h4><p>Box Box1；//声明Box1.类型为Box</p>
<p>Box Box2；//声明为Box2，类型为Box</p>
<p>它们各自的数据成员</p>
<h4 id="访问数据成员"><a href="#访问数据成员" class="headerlink" title="访问数据成员"></a>访问数据成员</h4><p>pubilc类型可以使用成员访问运算符  <strong>.</strong>  来访问</p>
<p>成员函数可以在类内部定义，或者单独使用范围解析运算符**::**来定义</p>
<p>一个简单的例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;iostream&gt;</span><br><span class="line">#include&lt;cstring&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Box</span><br><span class="line">&#123;</span><br><span class="line">	public:</span><br><span class="line">		double length;//长度 </span><br><span class="line">		double breadth;//宽度</span><br><span class="line">		double heigth;//高度</span><br><span class="line">		//成员函数的声明</span><br><span class="line">		double get(void);/*可以直接在此处定义</span><br><span class="line">		&#123;</span><br><span class="line">			return length*breadth*heigth;	</span><br><span class="line">		&#125; */</span><br><span class="line">		</span><br><span class="line">		void set(double len,double bre,double hei); </span><br><span class="line">&#125;;</span><br><span class="line">double Box::get(void)</span><br><span class="line">&#123;</span><br><span class="line">	return length*breadth*heigth;	</span><br><span class="line">&#125; </span><br><span class="line">void Box::set(double len,double bre,double hei)</span><br><span class="line">&#123;</span><br><span class="line">	length=len;</span><br><span class="line">	breadth=bre;</span><br><span class="line">	heigth=hei;</span><br><span class="line">&#125;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	Box Box1;//声明Box1，类型为Box </span><br><span class="line">	Box Box2;</span><br><span class="line">	double volume=0.0;//体积</span><br><span class="line">	</span><br><span class="line">	//Box1</span><br><span class="line">	Box1.length=5.0;</span><br><span class="line">	Box1.breadth=6.0;</span><br><span class="line">	Box1.heigth=7.0;</span><br><span class="line">	volume=Box1.heigth*Box1.length*Box1.breadth;</span><br><span class="line">	cout&lt;&lt;&quot;Box1的体积为:&quot;&lt;&lt;volume&lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	//Box2</span><br><span class="line">	Box2.set(16.0,8.0,12.0);</span><br><span class="line">	volume=Box2.get(); </span><br><span class="line">	cout&lt;&lt;&quot;Box2的体积为:&quot;&lt;&lt;volume&lt;&lt;endl; </span><br><span class="line">	</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>prviate和protected的成员不能直接使用成员访问符来直接访问</p>
<h4 id="类访问修饰符"><a href="#类访问修饰符" class="headerlink" title="类访问修饰符"></a>类访问修饰符</h4><p>有pubilc,prviate,protected三种</p>
<p>pubilc类型上述例子已经说明</p>
<p><strong>private</strong>的变量或函数在类的外部不可访问，甚至不可查看，只有类和友元函数可以访问</p>
<p>实际操作中，一般会在私有区域定义数据，在公有屈原定义相关函数</p>
<p>举个例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Box</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      double length;</span><br><span class="line">      void setWidth( double wid );</span><br><span class="line">      double getWidth( void );</span><br><span class="line">   private:</span><br><span class="line">      double width;</span><br><span class="line">&#125;;</span><br><span class="line">// 成员函数定义</span><br><span class="line">double Box::getWidth(void)</span><br><span class="line">&#123;</span><br><span class="line">    return width ;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">void Box::setWidth( double wid )</span><br><span class="line">&#123;</span><br><span class="line">    width = wid;</span><br><span class="line">&#125;</span><br><span class="line">// 程序的主函数</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Box box;</span><br><span class="line"> </span><br><span class="line">   // 不使用成员函数设置长度</span><br><span class="line">   box.length = 10.0; // OK: 因为 length 是公有的</span><br><span class="line">   cout &lt;&lt; &quot;Length of box : &quot; &lt;&lt; box.length &lt;&lt;endl;</span><br><span class="line"> </span><br><span class="line">   // 不使用成员函数设置宽度</span><br><span class="line">   // box.width = 10.0; // Error: 因为 width 是私有的</span><br><span class="line">   box.setWidth(10.0);  // 使用成员函数设置宽度</span><br><span class="line">   cout &lt;&lt; &quot;Width of box : &quot; &lt;&lt; box.getWidth() &lt;&lt;endl;</span><br><span class="line">   </span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>只能通过pubilc的函数去访问prviate的数据</p>
<p><strong>protected</strong></p>
<p>与上一个类型十分类似，但成员在派生类(子类)里面是可以访问的</p>
<p><strong>继承中的特点</strong>(继承在后续内容中有介绍)</p>
<ul>
<li>1.<strong>public 继承：</strong>基类 public 成员，protected 成员，private 成员的访问属性在派生类中分别变成：public, protected, private</li>
<li>2.<strong>protected 继承：</strong>基类 public 成员，protected 成员，private 成员的访问属性在派生类中分别变成：protected, protected, private</li>
<li>3.<strong>private 继承：</strong>基类 public 成员，protected 成员，private 成员的访问属性在派生类中分别变成：private, private, private</li>
</ul>
<h4 id="类的构造函数-amp-折构函数"><a href="#类的构造函数-amp-折构函数" class="headerlink" title="类的构造函数&amp;折构函数"></a>类的构造函数&amp;折构函数</h4><p><strong>构造函数</strong></p>
<p>类的构造函数是类的一种特殊的成员函数，会在每次创建类的新对象时执行</p>
<p>构造函数的名称与类的名称一样的，并且不会返回任何类型，包括void</p>
<p>举个例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Line</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      void setLength( double len );</span><br><span class="line">      double getLength( void );</span><br><span class="line">      Line();  // 这是构造函数</span><br><span class="line">   private:</span><br><span class="line">      double length;</span><br><span class="line">&#125;;</span><br><span class="line">// 成员函数定义，包括构造函数</span><br><span class="line">Line::Line(void)</span><br><span class="line">&#123;</span><br><span class="line">    cout &lt;&lt; &quot;Object is being created&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">void Line::setLength( double len )</span><br><span class="line">&#123;</span><br><span class="line">    length = len;</span><br><span class="line">&#125;</span><br><span class="line">double Line::getLength( void )</span><br><span class="line">&#123;</span><br><span class="line">    return length;</span><br><span class="line">&#125;</span><br><span class="line">// 程序的主函数</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Line line;//输出&#x27;obeject is being created&#x27;</span><br><span class="line">   // 设置长度</span><br><span class="line">   line.setLength(6.0); </span><br><span class="line">   cout &lt;&lt; &quot;Length of line : &quot; &lt;&lt; line.getLength() &lt;&lt;endl;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Object is being created</span><br><span class="line">Length of line : 6</span><br></pre></td></tr></table></figure>

<p>默认的构造函数没有参数，可以添加</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">......</span><br><span class="line">class Line</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      void setLength( double len );</span><br><span class="line">      double getLength( void );</span><br><span class="line">      Line(double len);  // 这是构造函数</span><br><span class="line">   private:</span><br><span class="line">      double length;</span><br><span class="line">&#125;;</span><br><span class="line">// 成员函数定义，包括构造函数</span><br><span class="line">Line::Line( double len)</span><br><span class="line">&#123;</span><br><span class="line">    cout &lt;&lt; &quot;Object is being created, length = &quot; &lt;&lt; len &lt;&lt; endl;</span><br><span class="line">    length = len;</span><br><span class="line">&#125;</span><br><span class="line">.........</span><br><span class="line">// 程序的主函数</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Line line(10.0);</span><br><span class="line">   // 获取默认设置的长度</span><br><span class="line">   cout &lt;&lt; &quot;Length of line : &quot; &lt;&lt; line.getLength() &lt;&lt;endl;</span><br><span class="line">   // 再次设置长度</span><br><span class="line">   line.setLength(6.0); </span><br><span class="line">   cout &lt;&lt; &quot;Length of line : &quot; &lt;&lt; line.getLength() &lt;&lt;endl;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Object is being created, length = 10</span><br><span class="line">Length of line : 10</span><br><span class="line">Length of line : 6</span><br></pre></td></tr></table></figure>

<p>使用初始化列表来初始化字段</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Line::Line( double len): length(len)</span><br><span class="line">&#123;</span><br><span class="line">    cout &lt;&lt; &quot;Object is being created, length = &quot; &lt;&lt; len &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">等价于</span><br><span class="line">Line::Line( double len)</span><br><span class="line">&#123;</span><br><span class="line">    length = len;</span><br><span class="line">    cout &lt;&lt; &quot;Object is being created, length = &quot; &lt;&lt; len &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">C::C( double a, double b, double c): X(a), Y(b), Z(c)</span><br><span class="line">&#123;</span><br><span class="line">  ....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>类的折构函数</strong></p>
<p>会在每次删除创建的对象时执行</p>
<p>折构函数名称与类的名称完全相同，但会在前面加(~)作为前缀，不返回任何值，也不能带有任何参数，此函数有助于在跳出程序前释放资源</p>
<p>例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Line</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      void setLength( double len );</span><br><span class="line">      double getLength( void );</span><br><span class="line">      Line();   // 这是构造函数声明</span><br><span class="line">      ~Line();  // 这是析构函数声明</span><br><span class="line"> </span><br><span class="line">   private:</span><br><span class="line">      double length;</span><br><span class="line">&#125;;</span><br><span class="line">// 成员函数定义，包括构造函数</span><br><span class="line">Line::Line(void)</span><br><span class="line">&#123;</span><br><span class="line">    cout &lt;&lt; &quot;Object is being created&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">Line::~Line(void)</span><br><span class="line">&#123;</span><br><span class="line">    cout &lt;&lt; &quot;Object is being deleted&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">void Line::setLength( double len )</span><br><span class="line">&#123;</span><br><span class="line">    length = len;</span><br><span class="line">&#125;</span><br><span class="line">double Line::getLength( void )</span><br><span class="line">&#123;</span><br><span class="line">    return length;</span><br><span class="line">&#125;</span><br><span class="line">// 程序的主函数</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Line line;</span><br><span class="line">   // 设置长度</span><br><span class="line">   line.setLength(6.0); </span><br><span class="line">   cout &lt;&lt; &quot;Length of line : &quot; &lt;&lt; line.getLength() &lt;&lt;endl;</span><br><span class="line">   return 0;//执行~Line里的内容</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">输出结果:</span><br><span class="line">Object is being created</span><br><span class="line">Length of line : 6</span><br><span class="line">Object is being deleted</span><br></pre></td></tr></table></figure>

<h4 id="拷贝构造函数"><a href="#拷贝构造函数" class="headerlink" title="拷贝构造函数"></a>拷贝构造函数</h4><p>在创建对象时，使用同一类中之前穿件的对象来初始化新创建的对象(复制)</p>
<p>拷贝构造函数通常用于:</p>
<p>1.通过使用另一个同类型的对象来初始化新创建的对象</p>
<p>2.复制对象把它作为参数传递给函数</p>
<p>3.复制对象，并从函数返回这个对象</p>
<p>如果在类中没有定义拷贝构造函数，编译器会自行定义一个。如果类带有指针变量，并有动态内存分配，则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">classname (const classname &amp;obj) &#123;</span><br><span class="line">   // 构造函数的主体</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>此处的obj是一个对象的引用，该对象是用于初始化另一个对象的</p>
<p>例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Line</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      int getLength( void );</span><br><span class="line">      Line( int len );             // 简单的构造函数</span><br><span class="line">      Line( const Line &amp;obj);      // 拷贝构造函数</span><br><span class="line">      ~Line();                     // 析构函数</span><br><span class="line"> </span><br><span class="line">   private:</span><br><span class="line">      int *ptr;</span><br><span class="line">&#125;;</span><br><span class="line">// 成员函数定义，包括构造函数</span><br><span class="line">Line::Line(int len)</span><br><span class="line">&#123;</span><br><span class="line">    cout&lt;&lt;&quot;调用构造函数&quot; &lt;&lt; endl;</span><br><span class="line">    // 为指针分配内存</span><br><span class="line">    ptr = new int;</span><br><span class="line">    *ptr = len;</span><br><span class="line">&#125;</span><br><span class="line">Line::Line(const Line &amp;obj)</span><br><span class="line">&#123;</span><br><span class="line">    cout &lt;&lt; &quot;调用拷贝构造函数并为指针 ptr 分配内存&quot; &lt;&lt; endl;</span><br><span class="line">    ptr = new int;</span><br><span class="line">    *ptr = *obj.ptr; // 拷贝值</span><br><span class="line">&#125;</span><br><span class="line">Line::~Line(void)</span><br><span class="line">&#123;</span><br><span class="line">    cout &lt;&lt; &quot;释放内存&quot; &lt;&lt; endl;</span><br><span class="line">    delete ptr;</span><br><span class="line">&#125;</span><br><span class="line">int Line::getLength( void )</span><br><span class="line">&#123;</span><br><span class="line">    return *ptr;</span><br><span class="line">&#125;</span><br><span class="line">void display(Line obj)</span><br><span class="line">&#123;</span><br><span class="line">   cout &lt;&lt; &quot;line 大小 : &quot; &lt;&lt; obj.getLength() &lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line">// 程序的主函数</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Line line1(10);</span><br><span class="line">   Line line2 = line1; // 这里也调用了拷贝构造函数</span><br><span class="line">   display(line1);</span><br><span class="line">   display(line2);</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">结果:</span><br><span class="line">调用构造函数</span><br><span class="line">调用拷贝构造函数并为指针 ptr 分配内存</span><br><span class="line">调用拷贝构造函数并为指针 ptr 分配内存</span><br><span class="line">line 大小 : 10</span><br><span class="line">释放内存</span><br><span class="line">调用拷贝构造函数并为指针 ptr 分配内存</span><br><span class="line">line 大小 : 10</span><br><span class="line">释放内存</span><br><span class="line">释放内存</span><br><span class="line">释放内存</span><br></pre></td></tr></table></figure>

<h4 id="友元函数"><a href="#友元函数" class="headerlink" title="友元函数"></a>友元函数</h4><p>类的友元函数时定义在类外部，但有权力访问类的private成员和protected成员</p>
<p>友元可以是一个函数，被称为友元函数，也可以是一个类，被称为友元类，此情况下，整个类及其所有成员都是友元</p>
<p>如果要声明函数为一个类的友元，需要在类定义中该函数原型前使用关键字 <strong>friend</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">class Box</span><br><span class="line">&#123;</span><br><span class="line">   double width;</span><br><span class="line">public:</span><br><span class="line">   double length;</span><br><span class="line">   friend void printWidth( Box box );</span><br><span class="line">   void setWidth( double wid );</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>声明类 ClassTwo 的所有成员函数作为类 ClassOne 的友元，需要在类 ClassOne 的定义中放置如下声明</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">friend class ClassTwo;</span><br></pre></td></tr></table></figure>

<h4 id="内联函数"><a href="#内联函数" class="headerlink" title="内联函数"></a>内联函数</h4><p>通常与类一起使用，如果一个函数时内联的，在编译时，编译器会把该函数的代码副本放置在每个调用该函数的地方</p>
<p>定义一个内联函数需要在函数名前放置 <strong>inline</strong></p>
<p>在类定义中的定义的函数都是内联函数</p>
<p>内联一般是用空间换取时间作为效率，所以一般都是小函数十行以内</p>
<p>注意:</p>
<p>1.在内联函数内不允许使用循环语句和开关语句；</p>
<p>2.内联函数的定义必须出现在内联函数第一次调用之前；</p>
<p>3.类结构中所在的类说明内部定义的函数是内联函数。</p>
<p>例子:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">inline int Max(int x, int y)</span><br><span class="line">&#123;</span><br><span class="line">   return (x &gt; y)? x : y;</span><br><span class="line">&#125;</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">   cout &lt;&lt; &quot;Max (20,10): &quot; &lt;&lt; Max(20,10) &lt;&lt; endl;</span><br><span class="line">   cout &lt;&lt; &quot;Max (0,200): &quot; &lt;&lt; Max(0,200) &lt;&lt; endl;</span><br><span class="line">   cout &lt;&lt; &quot;Max (100,1010): &quot; &lt;&lt; Max(100,1010) &lt;&lt; endl;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="this指针"><a href="#this指针" class="headerlink" title="this指针"></a>this指针</h4><p>C++中每一个对象都能通过this指针访问自己的地址，this指针是所有成员函数的隐含参数，在成员函数内部它可以用来指向调用对象</p>
<p>友元函数没有this指针，他不是类的对象</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Box</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      // 构造函数定义</span><br><span class="line">      Box(double l=2.0, double b=2.0, double h=2.0)</span><br><span class="line">      &#123;</span><br><span class="line">         cout &lt;&lt;&quot;Constructor called.&quot; &lt;&lt; endl;</span><br><span class="line">         length = l;</span><br><span class="line">         breadth = b;</span><br><span class="line">         height = h;</span><br><span class="line">      &#125;</span><br><span class="line">      double Volume()</span><br><span class="line">      &#123;</span><br><span class="line">         return length * breadth * height;</span><br><span class="line">      &#125;</span><br><span class="line">      int compare(Box box)</span><br><span class="line">      &#123;</span><br><span class="line">         return this-&gt;Volume() &gt; box.Volume();//-&gt;用于指向结构体的指针</span><br><span class="line">      &#125;</span><br><span class="line">   private:</span><br><span class="line">      double length;     // Length of a box</span><br><span class="line">      double breadth;    // Breadth of a box</span><br><span class="line">      double height;     // Height of a box</span><br><span class="line">&#125;;</span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">   Box Box1(3.3, 1.2, 1.5);    // Declare box1</span><br><span class="line">   Box Box2(8.5, 6.0, 2.0);    // Declare box2</span><br><span class="line">   if(Box1.compare(Box2))</span><br><span class="line">   &#123;</span><br><span class="line">      cout &lt;&lt; &quot;Box2 is smaller than Box1&quot; &lt;&lt;endl;</span><br><span class="line">   &#125;</span><br><span class="line">   else</span><br><span class="line">   &#123;</span><br><span class="line">      cout &lt;&lt; &quot;Box2 is equal to or larger than Box1&quot; &lt;&lt;endl;</span><br><span class="line">   &#125;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>关于</strong>this<strong>指针的一个精典回答:</strong></p>
<p>当你进入一个房子后，<br>你可以看见桌子、椅子、地板等，<br>但是房子你是看不到全貌了。</p>
<p>对于一个类的实例来说，<br>你可以看到它的成员函数、成员变量，<br>但是实例本身呢？<br>this是一个指针，它时时刻刻指向你这个实例本身</p>
<h4 id="类的静态成员"><a href="#类的静态成员" class="headerlink" title="类的静态成员"></a>类的静态成员</h4><p>可以使用static来把类成员定义为静态的，当我们声明类的成员为静态时，这意味着无论创建多少个类的对象，静态成员都只有一个副本。</p>
<p>静态成员在类的所有对象中是共享的。如果不存在其他的初始化语句，在创建第一个对象时，所有的静态数据都会被初始化为零。我们不能把静态成员的初始化放置在类的定义中，但是可以在类的外部通过使用范围解析运算符 <strong>::</strong> 来重新声明静态变量从而对它进行初始化</p>
<p>实例</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Box</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      static int objectCount;</span><br><span class="line">      // 构造函数定义</span><br><span class="line">      Box(double l=2.0, double b=2.0, double h=2.0)</span><br><span class="line">      &#123;</span><br><span class="line">         cout &lt;&lt;&quot;Constructor called.&quot; &lt;&lt; endl;</span><br><span class="line">         length = l;</span><br><span class="line">         breadth = b;</span><br><span class="line">         height = h;</span><br><span class="line">         // 每次创建对象时增加 1</span><br><span class="line">         objectCount++;</span><br><span class="line">      &#125;</span><br><span class="line">      double Volume()</span><br><span class="line">      &#123;</span><br><span class="line">         return length * breadth * height;</span><br><span class="line">      &#125;</span><br><span class="line">   private:</span><br><span class="line">      double length;     // 长度</span><br><span class="line">      double breadth;    // 宽度</span><br><span class="line">      double height;     // 高度</span><br><span class="line">&#125;;</span><br><span class="line">// 初始化类 Box 的静态成员</span><br><span class="line">int Box::objectCount = 0;</span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">   Box Box1(3.3, 1.2, 1.5);    // 声明 box1</span><br><span class="line">   Box Box2(8.5, 6.0, 2.0);    // 声明 box2</span><br><span class="line">   // 输出对象的总数</span><br><span class="line">   cout &lt;&lt; &quot;Total objects: &quot; &lt;&lt; Box::objectCount &lt;&lt; endl;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>静态成员函数</strong></p>
<p>如果把函数成员声明为静态的，就可以把函数与类的任何特定对象独立开来。静态成员函数即使在类对象不存在的情况下也能被调用，<strong>静态函数</strong>只要使用类名加范围解析运算符 <strong>::</strong> 就可以访问。</p>
<p>静态成员函数只能访问静态成员数据、其他静态成员函数和类外部的其他函数。</p>
<p>静态成员函数有一个类范围，他们不能访问类的 this 指针。您可以使用静态成员函数来判断类的某些对象是否已被创建。</p>
<blockquote>
<p><strong>静态成员函数与普通成员函数的区别：</strong></p>
<ul>
<li>静态成员函数没有 this 指针，只能访问静态成员（包括静态成员变量和静态成员函数）。</li>
<li>普通成员函数有 this 指针，可以访问类中的任意成员；而静态成员函数没有 this 指针</li>
</ul>
</blockquote>
<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><p>继承允许我们依据一个类来定义另一个类，当创建一个类时不需要重现编写新的数据成员和成员函数，只需指定新建的类继承了一个已有的类的成员即可，这个已有的类称为 <strong>基类</strong>，新建的类称为 <strong>派生类</strong></p>
<p>例如，哺乳动物是动物，狗是哺乳动物，因此狗是动物</p>
<p><img src="https://s2.loli.net/2022/03/12/ZdoOB5hq3UNv4AT.png" alt="2号.png"></p>
<p> 代码如下</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">//基类</span><br><span class="line">class Animal&#123;</span><br><span class="line">	//eat()函数</span><br><span class="line">	//sleep()函数</span><br><span class="line">&#125;；</span><br><span class="line">//派生类</span><br><span class="line">class Dog:public Animal&#123;</span><br><span class="line">	//bark函数()</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>一个类可以派生自多个类，可以从多个基类继承数据和函数</p>
<p>定义一个派生类，可以使用一个类派生列表来指定基类，类派生列表以一个或多个基类命名</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">class derived-class: access-specifier base-class</span><br></pre></td></tr></table></figure>

<p>访问修饰符 access-specifier 是 <strong>public、protected</strong> 或 <strong>private</strong> 其中的一个，base-class 是之前定义过的某个类的名称。如果未使用访问修饰符 access-specifier，则默认为 private。</p>
<p>举个例子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">// 基类</span><br><span class="line">class Shape </span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      void setWidth(int w)</span><br><span class="line">      &#123;</span><br><span class="line">         width = w;</span><br><span class="line">      &#125;</span><br><span class="line">      void setHeight(int h)</span><br><span class="line">      &#123;</span><br><span class="line">         height = h;</span><br><span class="line">      &#125;</span><br><span class="line">   protected:</span><br><span class="line">      int width;</span><br><span class="line">      int height;</span><br><span class="line">&#125;;</span><br><span class="line">// 派生类</span><br><span class="line">class Rectangle: public Shape</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      int getArea()</span><br><span class="line">      &#123; </span><br><span class="line">         return (width * height); </span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">   Rectangle Rect;</span><br><span class="line">   Rect.setWidth(5);</span><br><span class="line">   Rect.setHeight(7);</span><br><span class="line">   // 输出对象的面积</span><br><span class="line">   cout &lt;&lt; &quot;Total area: &quot; &lt;&lt; Rect.getArea() &lt;&lt; endl;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>最终得到Total area: 35</p>
<h4 id="访问的控制和继承"><a href="#访问的控制和继承" class="headerlink" title="访问的控制和继承"></a>访问的控制和继承</h4><table>
<thead>
<tr>
<th>访问</th>
<th>public</th>
<th>protected</th>
<th>private</th>
</tr>
</thead>
<tbody><tr>
<td>同一个类</td>
<td>yes</td>
<td>yes</td>
<td>yes</td>
</tr>
<tr>
<td>派生类</td>
<td>yes</td>
<td>yes</td>
<td>no</td>
</tr>
<tr>
<td>外部的类</td>
<td>yes</td>
<td>no</td>
<td>no</td>
</tr>
</tbody></table>
<p>一个派生类继承了所有的基类方法，但以下情况除外</p>
<ul>
<li>基类的构造函数、析构函数和拷贝构造函数</li>
<li>基类的重载运算符</li>
<li>基类的友元函数</li>
</ul>
<h4 id="继承类型"><a href="#继承类型" class="headerlink" title="继承类型"></a>继承类型</h4><p>一个类派生自基类，可以被继承为Public，protected或private几种类型</p>
<p>几乎不使用protected或private继承，使用不同类型的继承时遵循以下原则</p>
<p><strong>公有继承（public）：</strong>当一个类派生自<strong>公有</strong>基类时，基类的<strong>公有</strong>成员也是派生类的<strong>公有</strong>成员，基类的<strong>保护</strong>成员也是派生类的<strong>保护</strong>成员，基类的<strong>私有</strong>成员不能直接被派生类访问，但是可以通过调用基类的<strong>公有</strong>和<strong>保护</strong>成员来访问。</p>
<p><strong>保护继承（protected）：</strong> 当一个类派生自<strong>保护</strong>基类时，基类的<strong>公有</strong>和<strong>保护</strong>成员将成为派生类的<strong>保护</strong>成员。</p>
<p><strong>私有继承（private）：</strong>当一个类派生自<strong>私有</strong>基类时，基类的<strong>公有</strong>和<strong>保护</strong>成员将成为派生类的<strong>私有</strong>成员</p>
<h4 id="多继承"><a href="#多继承" class="headerlink" title="多继承"></a>多继承</h4><p>就是一个子类可以有多个父类，继承多个父类的特性，语法如下</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">class &lt;派生类名&gt;:&lt;继承方式1&gt;&lt;基类名1&gt;,&lt;继承方式2&gt;&lt;基类名2&gt;,…</span><br><span class="line">&#123;</span><br><span class="line">&lt;派生类类体&gt;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>举例</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">#include&lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class shape</span><br><span class="line">&#123;</span><br><span class="line">	public:</span><br><span class="line">		void setwidth(int w)</span><br><span class="line">		&#123;</span><br><span class="line">			width=w;	</span><br><span class="line">		&#125;	</span><br><span class="line">		void setheight(int h)</span><br><span class="line">		&#123;</span><br><span class="line">			heigth=h;</span><br><span class="line">		&#125;</span><br><span class="line">	protected:</span><br><span class="line">		int width;</span><br><span class="line">		int heigth;</span><br><span class="line">&#125;;</span><br><span class="line">class pointcost</span><br><span class="line">&#123;</span><br><span class="line">	public:</span><br><span class="line">		int getcost(int area)</span><br><span class="line">		&#123;</span><br><span class="line">			return area*70;</span><br><span class="line">		&#125;</span><br><span class="line">&#125;;</span><br><span class="line">class rectangle:public shape,public pointcost</span><br><span class="line">&#123;</span><br><span class="line">	public:</span><br><span class="line">		int getarea()</span><br><span class="line">		&#123;</span><br><span class="line">			return (width*heigth);</span><br><span class="line">		&#125;</span><br><span class="line">&#125;;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	rectangle rect;</span><br><span class="line">	int area;</span><br><span class="line">	</span><br><span class="line">	rect.setwidth(5);</span><br><span class="line">	rect.setheight(7);</span><br><span class="line">	</span><br><span class="line">	area=rect.getarea();</span><br><span class="line">	</span><br><span class="line">	cout&lt;&lt;&quot;面积为:&quot;&lt;&lt;rect.getarea()&lt;&lt;endl;</span><br><span class="line">	cout&lt;&lt;&quot;价格为:&quot;&lt;&lt;rect.getcost(area)&lt;&lt;endl;  </span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="重载运算符和重载函数"><a href="#重载运算符和重载函数" class="headerlink" title="重载运算符和重载函数"></a>重载运算符和重载函数</h3><p>C++允许在同一作用域中的某个函数和运算符指定多个定义，分别称为函数重载和运算符重载</p>
<p>当调用一个<strong>重载函数</strong>或<strong>重载运算符</strong>时，编译器通过把所使用的参数类型与定义中的参数类型进行比较，决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程，称为<strong>重载决策</strong>。</p>
<h4 id="函数重载"><a href="#函数重载" class="headerlink" title="函数重载"></a>函数重载</h4><p>同一个作用域中，可以声明几个功能类似的同名函数，但这些函数的参数形式(个数，顺序，类型)不能相同</p>
<p>不能仅通过返回类型作为区别</p>
<p>例子:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class printData</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      void print(int i) &#123;</span><br><span class="line">        cout &lt;&lt; &quot;整数为: &quot; &lt;&lt; i &lt;&lt; endl;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      void print(double  f) &#123;</span><br><span class="line">        cout &lt;&lt; &quot;浮点数为: &quot; &lt;&lt; f &lt;&lt; endl;</span><br><span class="line">      &#125;</span><br><span class="line"> </span><br><span class="line">      void print(char c[]) &#123;</span><br><span class="line">        cout &lt;&lt; &quot;字符串为: &quot; &lt;&lt; c &lt;&lt; endl;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">   printData pd;</span><br><span class="line">   // 输出整数</span><br><span class="line">   pd.print(5);</span><br><span class="line">   // 输出浮点数</span><br><span class="line">   pd.print(500.263);</span><br><span class="line">   // 输出字符串</span><br><span class="line">   char c[] = &quot;Hello C++&quot;;</span><br><span class="line">   pd.print(c);</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="运算符重载"><a href="#运算符重载" class="headerlink" title="运算符重载"></a>运算符重载</h4><p>可以重定义或重载大部分C++内置的运算符，这样就能使用自定义的运算符</p>
<p>重载的运算符是带有特殊名称的函数，函数名是由关键字operator和其后要重载的运算符符号构成的</p>
<p>与其他函数一样，重载运算符有一个返回类型和一个参数列表</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Box operator+(const Box&amp;);</span><br></pre></td></tr></table></figure>

<p>声明加法运算符用于把两个 Box 对象相加，返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数，那么我们需要为每次操作传递两个参数，如下所示：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Box operator+(const Box&amp;, const Box&amp;);</span><br></pre></td></tr></table></figure>

<p>例子:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Box</span><br><span class="line">&#123;</span><br><span class="line">   public:</span><br><span class="line">      double getVolume(void)</span><br><span class="line">      &#123;</span><br><span class="line">         return length * breadth * height;</span><br><span class="line">      &#125;</span><br><span class="line">      void setLength( double len )</span><br><span class="line">      &#123;</span><br><span class="line">          length = len;</span><br><span class="line">      &#125;</span><br><span class="line">      void setBreadth( double bre )</span><br><span class="line">      &#123;</span><br><span class="line">          breadth = bre;</span><br><span class="line">      &#125;</span><br><span class="line">      void setHeight( double hei )</span><br><span class="line">      &#123;</span><br><span class="line">          height = hei;</span><br><span class="line">      &#125;</span><br><span class="line">      // 重载 + 运算符，用于把两个 Box 对象相加</span><br><span class="line">      Box operator+(const Box&amp; b)</span><br><span class="line">      &#123;</span><br><span class="line">         Box box;</span><br><span class="line">         box.length = this-&gt;length + b.length;</span><br><span class="line">         box.breadth = this-&gt;breadth + b.breadth;</span><br><span class="line">         box.height = this-&gt;height + b.height;</span><br><span class="line">         return box;</span><br><span class="line">      &#125;</span><br><span class="line">   private:</span><br><span class="line">      double length;      // 长度</span><br><span class="line">      double breadth;     // 宽度</span><br><span class="line">      double height;      // 高度</span><br><span class="line">&#125;;</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Box Box1;                // 声明 Box1，类型为 Box</span><br><span class="line">   Box Box2;                // 声明 Box2，类型为 Box</span><br><span class="line">   Box Box3;                // 声明 Box3，类型为 Box</span><br><span class="line">   double volume = 0.0;     // 把体积存储在该变量中</span><br><span class="line"> </span><br><span class="line">   // Box1 详述</span><br><span class="line">   Box1.setLength(6.0); </span><br><span class="line">   Box1.setBreadth(7.0); </span><br><span class="line">   Box1.setHeight(5.0);</span><br><span class="line">   </span><br><span class="line">   // Box2 详述</span><br><span class="line">   Box2.setLength(12.0); </span><br><span class="line">   Box2.setBreadth(13.0); </span><br><span class="line">   Box2.setHeight(10.0);</span><br><span class="line">   </span><br><span class="line">   // Box1 的体积</span><br><span class="line">   volume = Box1.getVolume();</span><br><span class="line">   cout &lt;&lt; &quot;Volume of Box1 : &quot; &lt;&lt; volume &lt;&lt;endl;</span><br><span class="line">  </span><br><span class="line">   // Box2 的体积</span><br><span class="line">   volume = Box2.getVolume();</span><br><span class="line">   cout &lt;&lt; &quot;Volume of Box2 : &quot; &lt;&lt; volume &lt;&lt;endl;</span><br><span class="line">   </span><br><span class="line">   // 把两个对象相加，得到 Box3</span><br><span class="line">   Box3 = Box1 + Box2;</span><br><span class="line">   </span><br><span class="line">   // Box3 的体积</span><br><span class="line">   volume = Box3.getVolume();</span><br><span class="line">   cout &lt;&lt; &quot;Volume of Box3 : &quot; &lt;&lt; volume &lt;&lt;endl;</span><br><span class="line">  </span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="可重载运算符-不可重载运算符"><a href="#可重载运算符-不可重载运算符" class="headerlink" title="可重载运算符/不可重载运算符"></a>可重载运算符/不可重载运算符</h4><p>下面是可重载的运算符列表：</p>
<table>
<thead>
<tr>
<th>双目算术运算符</th>
<th>+ (加)，-(减)，*(乘)，/(除)，% (取模)</th>
</tr>
</thead>
<tbody><tr>
<td>关系运算符</td>
<td>==(等于)，!= (不等于)，&lt; (小于)，&gt; (大于)，&lt;=(小于等于)，&gt;=(大于等于)</td>
</tr>
<tr>
<td>逻辑运算符</td>
<td>||(逻辑或)，&amp;&amp;(逻辑与)，!(逻辑非)</td>
</tr>
<tr>
<td>单目运算符</td>
<td>+ (正)，-(负)，*(指针)，&amp;(取地址)</td>
</tr>
<tr>
<td>自增自减运算符</td>
<td>++(自增)，–(自减)</td>
</tr>
<tr>
<td>位运算符</td>
<td>| (按位或)，&amp; (按位与)，~(按位取反)，^(按位异或),，&lt;&lt; (左移)，&gt;&gt;(右移)</td>
</tr>
<tr>
<td>赋值运算符</td>
<td>=, +=, -=, *=, /= , % = , &amp;=, |=, ^=, &lt;&lt;=, &gt;&gt;=</td>
</tr>
<tr>
<td>空间申请与释放</td>
<td>new, delete, new[ ] , delete[]</td>
</tr>
<tr>
<td>其他运算符</td>
<td>()(函数调用)，-&gt;(成员访问)，,(逗号)，<a href="%E4%B8%8B%E6%A0%87"></a></td>
</tr>
</tbody></table>
<p>下面是不可重载的运算符列表：</p>
<ul>
<li>.：成员访问运算符</li>
<li>.*, -&gt;*：成员指针访问运算符</li>
<li>::：域运算符</li>
<li>sizeof：长度运算符</li>
<li>?:：条件运算符</li>
<li>#： 预处理符号</li>
</ul>
<h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><p><strong>多态</strong>按字面的意思就是多种形态。当类之间存在层次结构，并且类之间是通过继承关联时，就会用到多态</p>
<p>C++ 多态意味着调用成员函数时，会根据调用函数的对象的类型来执行不同的函数</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt; </span><br><span class="line">using namespace std;</span><br><span class="line"> </span><br><span class="line">class Shape &#123;</span><br><span class="line">   protected:</span><br><span class="line">      int width, height;</span><br><span class="line">   public:</span><br><span class="line">      Shape( int a=0, int b=0)</span><br><span class="line">      &#123;</span><br><span class="line">         width = a;</span><br><span class="line">         height = b;</span><br><span class="line">      &#125;</span><br><span class="line">      int area()</span><br><span class="line">      &#123;</span><br><span class="line">         cout &lt;&lt; &quot;Parent class area :&quot; &lt;&lt;endl;</span><br><span class="line">         return 0;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br><span class="line">class Rectangle: public Shape&#123;</span><br><span class="line">   public:</span><br><span class="line">      Rectangle( int a=0, int b=0):Shape(a, b) &#123; &#125;</span><br><span class="line">      int area ()</span><br><span class="line">      &#123; </span><br><span class="line">         cout &lt;&lt; &quot;Rectangle class area :&quot; &lt;&lt;endl;</span><br><span class="line">         return (width * height); </span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br><span class="line">class Triangle: public Shape&#123;</span><br><span class="line">   public:</span><br><span class="line">      Triangle( int a=0, int b=0):Shape(a, b) &#123; &#125;</span><br><span class="line">      int area ()</span><br><span class="line">      &#123; </span><br><span class="line">         cout &lt;&lt; &quot;Triangle class area :&quot; &lt;&lt;endl;</span><br><span class="line">         return (width * height / 2); </span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br><span class="line">// 程序的主函数</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Shape *shape;</span><br><span class="line">   Rectangle rec(10,7);</span><br><span class="line">   Triangle  tri(10,5);</span><br><span class="line"> </span><br><span class="line">   // 存储矩形的地址</span><br><span class="line">   shape = &amp;rec;</span><br><span class="line">   // 调用矩形的求面积函数 area</span><br><span class="line">   shape-&gt;area();</span><br><span class="line"> </span><br><span class="line">   // 存储三角形的地址</span><br><span class="line">   shape = &amp;tri;</span><br><span class="line">   // 调用三角形的求面积函数 area</span><br><span class="line">   shape-&gt;area();</span><br><span class="line">   </span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>它会产生下列结果：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Parent class area :</span><br><span class="line">Parent class area :</span><br></pre></td></tr></table></figure>

<p>错误输出的原因是，调用函数 area() 被编译器设置为基类中的版本，这就是的<strong>静态多态</strong>，或<strong>静态链接</strong> - 函数调用在程序执行前就准备好了。这也被称为<strong>早绑定</strong>，因为 area() 函数在程序编译期间就已经设置好了。</p>
<p>在 Shape 类中，area() 的声明前放置关键字 <strong>virtual</strong>，如下所示：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">class Shape &#123;</span><br><span class="line">   protected:</span><br><span class="line">      int width, height;</span><br><span class="line">   public:</span><br><span class="line">      Shape( int a=0, int b=0)</span><br><span class="line">      &#123;</span><br><span class="line">         width = a;</span><br><span class="line">         height = b;</span><br><span class="line">      &#125;</span><br><span class="line">      virtual int area()</span><br><span class="line">      &#123;</span><br><span class="line">         cout &lt;&lt; &quot;Parent class area :&quot; &lt;&lt;endl;</span><br><span class="line">         return 0;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>修改后,它会产生以下结果：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Rectangle class area :</span><br><span class="line">Triangle class area :</span><br></pre></td></tr></table></figure>

<h4 id="虚函数"><a href="#虚函数" class="headerlink" title="虚函数"></a>虚函数</h4><p>是基类中使用virtual声明的函数，在派生类中重新定义基类中定义的虚函数时，会告诉编译器不要静态链接到改函数，我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数，这种操作被称为<strong>动态链接</strong>，或<strong>后期绑定</strong>。</p>
<h4 id="纯虚函数"><a href="#纯虚函数" class="headerlink" title="纯虚函数"></a>纯虚函数</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">class Shape &#123;</span><br><span class="line">   protected:</span><br><span class="line">      int width, height;</span><br><span class="line">   public:</span><br><span class="line">      Shape( int a=0, int b=0)</span><br><span class="line">      &#123;</span><br><span class="line">         width = a;</span><br><span class="line">         height = b;</span><br><span class="line">      &#125;</span><br><span class="line">      //纯虚函数</span><br><span class="line">      virtual int area() = 0;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>= 0 告诉编译器，函数没有主体，上面的虚函数是<strong>纯虚函数</strong>。</p>
<h3 id=""><a href="#" class="headerlink" title=""></a></h3><h3 id="数据抽象"><a href="#数据抽象" class="headerlink" title="数据抽象"></a>数据抽象</h3><p>数据抽象是指只向外界提供关键信息，隐藏后台的实现细节，只表现必要的信息</p>
<p>比如我们可以用电视机切换频道，调整音量等，但我们并不知道它的内部如何实现，只需通过外端接口就可以实现如上操作</p>
<p>例如，在c++中可以使用sort()函数来排序，但并不需要知道此函数的算法</p>
<p>在 C++ 中，我们使用<strong>类</strong>来定义我们自己的抽象数据类型（ADT）。可以使用类 <strong>iostream</strong> 的 <strong>cout</strong> 对象来输出数据到标准输出，如下所示：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line"> </span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   cout &lt;&lt; &quot;Hello C++&quot; &lt;&lt;endl;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>但并不需要理解cout如何在屏幕上显示文本，只需要知道公共接口</p>
<h4 id="访问标签强制抽象"><a href="#访问标签强制抽象" class="headerlink" title="访问标签强制抽象"></a>访问标签强制抽象</h4><p> C++ 中，我们使用访问标签来定义类的抽象接口。一个类可以包含零个或多个访问标签：</p>
<ul>
<li>使用公共标签定义的成员都可以访问该程序的所有部分。一个类型的数据抽象视图是由它的公共成员来定义的。</li>
<li>使用私有标签定义的成员无法访问到使用类的代码。私有部分对使用类型的代码隐藏了实现细节。</li>
</ul>
<p>访问标签出现的频率没有限制。每个访问标签指定了紧随其后的成员定义的访问级别。指定的访问级别会一直有效，直到遇到下一个访问标签或者遇到类主体的关闭右括号为止。</p>
<h4 id="数据抽象实例"><a href="#数据抽象实例" class="headerlink" title="数据抽象实例"></a>数据抽象实例</h4><p>C++ 程序中，任何带有公有和私有成员的类都可以作为数据抽象的实例。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">class Adder&#123;</span><br><span class="line">   public:</span><br><span class="line">      // 构造函数</span><br><span class="line">      Adder(int i = 0)</span><br><span class="line">      &#123;</span><br><span class="line">        total = i;</span><br><span class="line">      &#125;</span><br><span class="line">      // 对外的接口</span><br><span class="line">      void addNum(int number)</span><br><span class="line">      &#123;</span><br><span class="line">          total += number;</span><br><span class="line">      &#125;</span><br><span class="line">      // 对外的接口</span><br><span class="line">      int getTotal()</span><br><span class="line">      &#123;</span><br><span class="line">          return total;</span><br><span class="line">      &#125;;</span><br><span class="line">   private:</span><br><span class="line">      // 对外隐藏的数据</span><br><span class="line">      int total;</span><br><span class="line">&#125;;</span><br><span class="line">int main( )</span><br><span class="line">&#123;</span><br><span class="line">   Adder a;</span><br><span class="line">   a.addNum(10);</span><br><span class="line">   a.addNum(20);</span><br><span class="line">   a.addNum(30);</span><br><span class="line">   cout &lt;&lt; &quot;Total &quot; &lt;&lt; a.getTotal() &lt;&lt;endl;</span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>公有成员 <strong>addNum</strong> 和 <strong>getTotal</strong> 是对外的接口</p>
<p>私有成员 <strong>total</strong> 是用户不需要了解的，但又是类能正常工作所必需的。</p>
<h3 id="数据封装"><a href="#数据封装" class="headerlink" title="数据封装"></a>数据封装</h3><p>C++程序有以下两个基本要素</p>
<ul>
<li><strong>程序语句（代码）：</strong>这是程序中执行动作的部分，它们被称为函数。</li>
<li><strong>程序数据：</strong>数据是程序的信息，会受到程序函数的影响。</li>
</ul>
<p>封装是吧数据和操作数据的函数绑定在一起的操作，避免外界的干扰和误用，数据封装引申出了另一个OPP概念，<strong>数据隐藏</strong></p>
<p>C++ 通过创建<strong>类</strong>来支持封装和数据隐藏（public、protected、private）</p>
<p>通常情况下，我们都会设置类成员状态为私有（private），除非我们真的需要将其暴露，这样才能保证良好的<strong>封装性</strong>。</p>
<p>这通常应用于数据成员，但它同样适用于所有成员，包括虚函数。</p>
<h3 id="接口-抽象类"><a href="#接口-抽象类" class="headerlink" title="接口(抽象类)"></a>接口(抽象类)</h3><p>接口描述了类的行为和功能，而不需要完成类的特定实现。</p>
<p>C++ 接口是使用<strong>抽象类</strong>来实现的，抽象类与数据抽象互不混淆，数据抽象是一个把实现细节与相关的数据分离开的概念。</p>
<p>如果类中至少有一个函数被声明为纯虚函数，则这个类就是抽象类。纯虚函数是通过在声明中使用 “= 0” 来指定的</p>
<p>设计<strong>抽象类</strong>（通常称为 ABC）的目的，是为了给其他类提供一个可以继承的适当的基类。抽象类不能被用于实例化对象，它只能作为<strong>接口</strong>使用。如果试图实例化一个抽象类的对象，会导致编译错误。</p>
<p>因此，如果一个 ABC 的子类需要被实例化，则必须实现每个虚函数，这也意味着 C++ 支持使用 ABC 声明接口。如果没有在派生类中重写纯虚函数，就尝试实例化该类的对象，会导致编译错误。</p>
<p>可用于实例化对象的类被称为<strong>具体类</strong>。</p>
<p>实例</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">// 基类</span><br><span class="line">class Shape </span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">   // 提供接口框架的纯虚函数</span><br><span class="line">   virtual int getArea() = 0;</span><br><span class="line">   void setWidth(int w)</span><br><span class="line">   &#123;</span><br><span class="line">      width = w;</span><br><span class="line">   &#125;</span><br><span class="line">   void setHeight(int h)</span><br><span class="line">   &#123;</span><br><span class="line">      height = h;</span><br><span class="line">   &#125;</span><br><span class="line">protected:</span><br><span class="line">   int width;</span><br><span class="line">   int height;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line">// 派生类</span><br><span class="line">class Rectangle: public Shape</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">   int getArea()</span><br><span class="line">   &#123; </span><br><span class="line">      return (width * height); </span><br><span class="line">   &#125;</span><br><span class="line">&#125;;</span><br><span class="line">class Triangle: public Shape</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">   int getArea()</span><br><span class="line">   &#123; </span><br><span class="line">      return (width * height)/2; </span><br><span class="line">   &#125;</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">   Rectangle Rect;</span><br><span class="line">   Triangle  Tri;</span><br><span class="line"> </span><br><span class="line">   Rect.setWidth(5);</span><br><span class="line">   Rect.setHeight(7);</span><br><span class="line">   // 输出对象的面积</span><br><span class="line">   cout &lt;&lt; &quot;Total Rectangle area: &quot; &lt;&lt; Rect.getArea() &lt;&lt; endl;</span><br><span class="line"> </span><br><span class="line">   Tri.setWidth(5);</span><br><span class="line">   Tri.setHeight(7);</span><br><span class="line">   // 输出对象的面积</span><br><span class="line">   cout &lt;&lt; &quot;Total Triangle area: &quot; &lt;&lt; Tri.getArea() &lt;&lt; endl; </span><br><span class="line"> </span><br><span class="line">   return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>从上面的实例中，我们可以看到一个抽象类是如何定义一个接口 getArea()，两个派生类是如何通过不同的计算面积的算法来实现这个相同的函数。</p>
<p>2023.3.22</p>
<p>不知道有什么问题Gitee说我c++的知识(2)有违禁内容？</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">heit0ng</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://heit0ng.gitee.io/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/">https://heit0ng.gitee.io/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://heit0ng.gitee.io" target="_blank">heit0ng</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/04/07/C++qt/"><img class="prev-cover" src="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">C++Qt</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/22/%E6%94%BB%E9%98%B2%E4%B8%96%E7%95%8C%E7%9A%84wp/"><img class="next-cover" src="https://s2.loli.net/2022/02/22/Wy6vfC23Rpqx8L4.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">攻防世界的wp(一)</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s2.loli.net/2022/01/28/RkXzVpjubg3yGEI.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">heit0ng</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">11</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">0</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#C-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.</span> <span class="toc-text">C++面向对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#c-%E7%B1%BB-amp-%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.1.</span> <span class="toc-text">c++类&amp;对象</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#c-%E7%B1%BB%E5%AE%9A%E4%B9%89"><span class="toc-number">1.1.1.</span> <span class="toc-text">c++类定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89C-%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.1.2.</span> <span class="toc-text">定义C++对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E6%95%B0%E6%8D%AE%E6%88%90%E5%91%98"><span class="toc-number">1.1.3.</span> <span class="toc-text">访问数据成员</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E8%AE%BF%E9%97%AE%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-number">1.1.4.</span> <span class="toc-text">类访问修饰符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-amp-%E6%8A%98%E6%9E%84%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.5.</span> <span class="toc-text">类的构造函数&amp;折构函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8B%B7%E8%B4%9D%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.6.</span> <span class="toc-text">拷贝构造函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%8B%E5%85%83%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.7.</span> <span class="toc-text">友元函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%86%85%E8%81%94%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.8.</span> <span class="toc-text">内联函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#this%E6%8C%87%E9%92%88"><span class="toc-number">1.1.9.</span> <span class="toc-text">this指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E9%9D%99%E6%80%81%E6%88%90%E5%91%98"><span class="toc-number">1.1.10.</span> <span class="toc-text">类的静态成员</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF"><span class="toc-number">1.2.</span> <span class="toc-text">继承</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E7%9A%84%E6%8E%A7%E5%88%B6%E5%92%8C%E7%BB%A7%E6%89%BF"><span class="toc-number">1.2.1.</span> <span class="toc-text">访问的控制和继承</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.2.2.</span> <span class="toc-text">继承类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A4%9A%E7%BB%A7%E6%89%BF"><span class="toc-number">1.2.3.</span> <span class="toc-text">多继承</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E8%BD%BD%E8%BF%90%E7%AE%97%E7%AC%A6%E5%92%8C%E9%87%8D%E8%BD%BD%E5%87%BD%E6%95%B0"><span class="toc-number">1.3.</span> <span class="toc-text">重载运算符和重载函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E9%87%8D%E8%BD%BD"><span class="toc-number">1.3.1.</span> <span class="toc-text">函数重载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-number">1.3.2.</span> <span class="toc-text">运算符重载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%AF%E9%87%8D%E8%BD%BD%E8%BF%90%E7%AE%97%E7%AC%A6-%E4%B8%8D%E5%8F%AF%E9%87%8D%E8%BD%BD%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.3.3.</span> <span class="toc-text">可重载运算符&#x2F;不可重载运算符</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E6%80%81"><span class="toc-number">1.4.</span> <span class="toc-text">多态</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%99%9A%E5%87%BD%E6%95%B0"><span class="toc-number">1.4.1.</span> <span class="toc-text">虚函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BA%AF%E8%99%9A%E5%87%BD%E6%95%B0"><span class="toc-number">1.4.2.</span> <span class="toc-text">纯虚函数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link"><span class="toc-number">1.5.</span> <span class="toc-text"></span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E6%8A%BD%E8%B1%A1"><span class="toc-number">1.6.</span> <span class="toc-text">数据抽象</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E6%A0%87%E7%AD%BE%E5%BC%BA%E5%88%B6%E6%8A%BD%E8%B1%A1"><span class="toc-number">1.6.1.</span> <span class="toc-text">访问标签强制抽象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E6%8A%BD%E8%B1%A1%E5%AE%9E%E4%BE%8B"><span class="toc-number">1.6.2.</span> <span class="toc-text">数据抽象实例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%B0%81%E8%A3%85"><span class="toc-number">1.7.</span> <span class="toc-text">数据封装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%A5%E5%8F%A3-%E6%8A%BD%E8%B1%A1%E7%B1%BB"><span class="toc-number">1.8.</span> <span class="toc-text">接口(抽象类)</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(2)/" title="j2ee(2)"><img src="https://s2.loli.net/2023/03/23/mTDuOeL67MFtgZs.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(2)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(2)/" title="j2ee(2)">j2ee(2)</a><time datetime="2023-03-22T15:50:01.860Z" title="发表于 2023-03-22 23:50:01">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/22/j2ee(1)/" title="j2ee(1)"><img src="https://s2.loli.net/2023/03/22/3KqCOLtHBdvhMFe.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="j2ee(1)"/></a><div class="content"><a class="title" href="/2023/03/22/j2ee(1)/" title="j2ee(1)">j2ee(1)</a><time datetime="2023-03-22T12:20:17.683Z" title="发表于 2023-03-22 20:20:17">2023-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统"><img src="https://s2.loli.net/2022/05/16/ae4E7n2D3I5mqkh.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C语言图书管理系统"/></a><div class="content"><a class="title" href="/2022/05/16/C%E8%AF%AD%E8%A8%80%E5%9B%BE%E4%B9%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="C语言图书管理系统">C语言图书管理系统</a><time datetime="2022-05-16T14:28:31.730Z" title="发表于 2022-05-16 22:28:31">2022-05-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/07/C++qt/" title="C++Qt"><img src="https://s2.loli.net/2022/04/07/Cyg9WQLqSNxX7Av.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C++Qt"/></a><div class="content"><a class="title" href="/2022/04/07/C++qt/" title="C++Qt">C++Qt</a><time datetime="2022-04-07T12:41:19.618Z" title="发表于 2022-04-07 20:41:19">2022-04-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)"><img src="https://s2.loli.net/2022/03/12/QrwRKYfGEmP3vVL.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="c++的知识(1)"/></a><div class="content"><a class="title" href="/2022/03/12/c++%E7%9A%84%E7%9F%A5%E8%AF%86(1)/" title="c++的知识(1)">c++的知识(1)</a><time datetime="2022-03-12T13:48:26.310Z" title="发表于 2022-03-12 21:48:26">2022-03-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By heit0ng</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>